package com.ql.gamelogm.controller;
import com.ql.gamelogm.entities.*;
import com.ql.gamelogm.service.LogService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class Echartscontroller {

    /*@Autowired
    private DataLogRepository dataLogRepository;*/
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private LogService logService;


    @RequestMapping(value = "/project")
    @ResponseBody
    public List<CountWarnLogs> myview(){
        List<WarnLog> warnLogList1=logService.findAllWarnLog();
        List<WarnLog> warnLogList=new ArrayList<WarnLog>();
        List<CountWarnLogs> countWarnLogsList=new ArrayList<CountWarnLogs>();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd ");
        //当前时间
        Date curdate = new Date(System.currentTimeMillis());
        //一周
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.DATE, -6);
        Date time = calendar.getTime();
        /*得到一周的数据*/
        for (int i = 0; i < warnLogList1.size(); i++) {
            if (warnLogList1.get(i).getDate().before(curdate) && warnLogList1.get(i).getDate().after(time)) {
                warnLogList.add(warnLogList1.get(i));
            }
        }
        //按时间顺序排序
        Collections.sort(warnLogList, new Comparator<WarnLog>() {
            @Override
            public int compare(WarnLog t1, WarnLog t2) {
                return t1.getDate().compareTo(t2.getDate());
            }
        });
        Date date=new Date();
        int size=warnLogList.size();
        /*System.out.println(size);*/
        int count=1,j,i,m=0,k;

        for(i=0;i<size;i++){
            for(j=i+1;j<size;j++){
                if((warnLogList.get(j).getDateres()).equals(warnLogList.get(i).getDateres())){
                    count++;
                    //j位置上的数和i所在位置上的数相等且j位置为最后一个位置
                    if(j==size-1){
                        CountWarnLogs countWarnLogs=new CountWarnLogs();
                        countWarnLogs.setDate(simpleDateFormat.format(warnLogList.get(i).getDateres()));
                        countWarnLogs.setCount(count);
                        countWarnLogsList.add(countWarnLogs);
                        i=j;
                        break;
                    }
                }else{
                    CountWarnLogs countWarnLogs=new CountWarnLogs();
                    countWarnLogs.setDate(simpleDateFormat.format(warnLogList.get(i).getDateres()));
                    countWarnLogs.setCount(count);
                    countWarnLogsList.add(countWarnLogs);
                    count=1;
                    i=j;
                }
            }
            for(k=0;k<countWarnLogsList.size();k++){
                m+=countWarnLogsList.get(k).getCount();
            }
            /*System.out.println(m);*/
            if(m!=size&&i==size-1){
                CountWarnLogs countWarnLogs=new CountWarnLogs();
                countWarnLogs.setDate(simpleDateFormat.format(warnLogList.get(i).getDateres()));
                countWarnLogs.setCount(1);
                countWarnLogsList.add(countWarnLogs);
            }
        }
        float avgrec=0,avgres=0;
        int temp=countWarnLogsList.get(0).getCount();//得到第一个月份的count;
        for(i=0;i<countWarnLogsList.size();i++){//多少个月份

            if(i==0){
                for(j=0;j<countWarnLogsList.get(i).getCount();j++){//一个月份中count为几
                    avgrec+=warnLogList.get(j).getAvgrecovery();
                    avgres+=warnLogList.get(j).getAvgresponse();
                }
                //取平均恢复时间和响应时间
                countWarnLogsList.get(i).setAvgrecov(avgrec/countWarnLogsList.get(i).getCount());
                countWarnLogsList.get(i).setAvgresp(avgres/countWarnLogsList.get(i).getCount());
                avgrec=0;
                avgres=0;
            }else{
                for(j=temp;j<countWarnLogsList.get(i).getCount()+temp;j++){
                    avgrec+=warnLogList.get(j).getAvgrecovery();
                    avgres+=warnLogList.get(j).getAvgresponse();
                }
                countWarnLogsList.get(i).setAvgrecov(avgrec/countWarnLogsList.get(i).getCount());
                countWarnLogsList.get(i).setAvgresp(avgres/countWarnLogsList.get(i).getCount());
                temp+=countWarnLogsList.get(i).getCount();
                avgrec=0;
                avgres=0;
            }

        }
        return countWarnLogsList;
    }
    @RequestMapping(value = "/LogWarn")
    public String  myEchartsDemo(){
        return "LogWarn";
    }
    @RequestMapping(value = "/getdata" )
    @ResponseBody
    public List<CountDataLog> myview1() throws ParseException {
        /*找出最近三天的告警数据和数量*/
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date curdate = new Date(System.currentTimeMillis());
        //三天前的时间
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.DATE, -2);
        Date time = calendar.getTime();
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.DATE, -1);
        Date time1 = calendar1.getTime();
        String date=simpleDateFormat.format(curdate);//date为今天
        String date2 = simpleDateFormat.format(time1);//date2为昨天
        String date1=simpleDateFormat.format(time);//date1为前天
        String date3="";
        String sql="select state,dateres,count(*) from warnlog where dateres between '"+date1+"' and '"+date+"'"+"group by state";
        List<WarnLogStateCount> warnLogStateCountList = jdbcTemplate.query(sql, (resultSet, rowNum) -> new WarnLogStateCount(
                resultSet.getString("state"),
                resultSet.getTimestamp("dateres"),
                resultSet.getInt("count(*)")
        ));
        int djs=0,yjs=0,ycl=0;//今天
        int djs1=0,yjs1=0,ycl1=0;//昨天
        int djs2=0,yjs2=0,ycl2=0;//前天
        for(int i=0;i<warnLogStateCountList.size();i++){
            date3=simpleDateFormat.format(warnLogStateCountList.get(i).getDateres());
            if(date3.equals(date1)){//前天的数据
                if(warnLogStateCountList.get(i).getState().equals("待接收")){
                    djs2+=warnLogStateCountList.get(i).getCount();
                }
                if(warnLogStateCountList.get(i).getState().equals("已接收")){
                    yjs2+=warnLogStateCountList.get(i).getCount();
                }
                if(warnLogStateCountList.get(i).getState().equals("已处理")){
                    ycl2+=warnLogStateCountList.get(i).getCount();
                }

            }else if(date3.equals(date2)){//昨天的数据
                if(warnLogStateCountList.get(i).getState().equals("待接收")){
                    djs1+=warnLogStateCountList.get(i).getCount();
                }
                if(warnLogStateCountList.get(i).getState().equals("已接收")){
                    yjs1+=warnLogStateCountList.get(i).getCount();
                }
                if(warnLogStateCountList.get(i).getState().equals("已处理")){
                    ycl1+=warnLogStateCountList.get(i).getCount();
                }

            }else{//今天的数据
                if(warnLogStateCountList.get(i).getState().equals("待接收")){
                    djs+=warnLogStateCountList.get(i).getCount();
                }
                if(warnLogStateCountList.get(i).getState().equals("待接收")){
                    yjs+=warnLogStateCountList.get(i).getCount();
                }
                if(warnLogStateCountList.get(i).getState().equals("待接收")){
                    ycl+=warnLogStateCountList.get(i).getCount();
                }
            }
        }

        /*新增日志*/
        String sql1 = "select date,count(*) from gamedata where date between '" + date1 + "' and '" + date + "'" + "group by date";
        List<NewLog> newLogList = jdbcTemplate.query(sql1, (resultSet, rowNum) -> new NewLog(
                resultSet.getTimestamp("date"),
                resultSet.getInt("count(*)")
        ));

        int xz1=0,xz2=0,xz3=0;//三天

        for(int i=0;i<newLogList.size();i++){
            date3=simpleDateFormat.format(newLogList.get(i).getDate());
            if(date3.equals(date1)){//前天的数据
                if(newLogList.get(i).getCount()!=1){
                    xz1+=newLogList.get(i).getCount();
                }
                xz1++;
            }else if(date3.equals(date2)){//昨天的数据
                if(newLogList.get(i).getCount()!=1){
                    xz2+=newLogList.get(i).getCount();
                }
                xz2++;
            }else{//今天的数据
                if(newLogList.get(i).getCount()!=1){
                    xz3+=newLogList.get(i).getCount();
                }
                xz3++;
            }
        }

        /*在线人数*/
        String sql2 = "select date,gamername from gamedata where date between '" + date1 + "' and '" + date + "'" + "group by gamername";
        List<GamerCount> gamerCountList = jdbcTemplate.query(sql2, (resultSet, rowNum) -> new GamerCount(
                resultSet.getTimestamp("date"),
                resultSet.getString("gamername")
        ));
        int zx1=0,zx2=0,zx3=0;//三天在线人数
        for(int i=0;i<gamerCountList.size();i++){
            date3=simpleDateFormat.format(gamerCountList.get(i).getDate());
            if(date3.equals(date1)){//前天的数据
                zx1++;
            }else if(date3.equals(date2)){//昨天的数据
                zx2++;
            }else{//今天的数据
                zx3++;
            }
        }
        /*System.out.println("今天在线："+zx3+",昨天在线："+zx2+",前天在线："+zx1);
        System.out.println("今天新增："+xz3+",昨天新增："+xz2+",前天新增："+xz1);
        System.out.println("今天待接收："+djs+",今天已接收："+yjs+",今天已处理："+ycl);
        System.out.println("昨天待接收："+djs1+",昨天已接收："+yjs1+",昨天已处理："+ycl1);
        System.out.println("前天待接收："+djs2+",前天已接收："+yjs2+",前天已处理："+ycl2);*/
        List<CountDataLog> countDataLogList=new ArrayList<CountDataLog>();
        //前天的数据
        CountDataLog countDataLog1=new CountDataLog(simpleDateFormat.parse(date1),xz1,zx1,djs2,yjs2,ycl2);
        //昨天的数据
        CountDataLog countDataLog2=new CountDataLog(simpleDateFormat.parse(date2),xz2,zx2,djs1,yjs1,ycl1);
        //今天的数据
        CountDataLog countDataLog3=new CountDataLog(simpleDateFormat.parse(date),xz3,zx3,djs,yjs,ycl);

        countDataLogList.add(countDataLog1);
        countDataLogList.add(countDataLog2);
        countDataLogList.add(countDataLog3);

        return countDataLogList;

    }
    @RequestMapping(value = "/LogAnalysis")
    public String  myEchartsDemo1(){
        return "LogAnalysis";
    }
}
